package copyengine.utils
{

	import org.puremvc.as3.patterns.mediator.Mediator;

	/**
	 * 支持嵌套结构的Mediator
	 * 类似于Display的结构，父Mediator负责initialize & dispose 子Mediator
	 */
	public class CENestMediatorBasic extends Mediator
	{
		private var subMediatorArray:Array;

		public function CENestMediatorBasic()
		{
			super(UUIDFactory.instance.generateUUID());
		}

		override final public function onRemove():void
		{
			doOnRemove();
			//subMediatorArray有可能没有被注册过
			if (subMediatorArray != null)
			{
				while (subMediatorArray.length > 0)
				{
					facade.removeMediator(subMediatorArray.pop().getMediatorName());
				}
			}
		}

		protected final function registerSubMediator():void
		{
			subMediatorArray = getSubMediatorArray();
			for each (var subMediator:CENestMediatorBasic in subMediatorArray)
			{
				facade.registerMediator(subMediator);
			}
		}

		protected function doOnRemove():void
		{
		}

		/**
		 * 在初始化时候Override该函数，且调用registerSubMediator
		 * 及在初始化阶段給该Mediator注册了相应的子Mediator
		 */
		protected function getSubMediatorArray():Array
		{
			return [];
		}

		protected function addSubMediator(_mediator:CENestMediatorBasic):void
		{
			//有可能没有子SubMediator
			if (subMediatorArray == null)
			{
				subMediatorArray = [];
			}
			subMediatorArray.push(_mediator);
			facade.registerMediator(_mediator);
		}


	}
}
